//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CmsCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (boost::shared_ptr<QuantLib::CmsCouponPricer>& childNative, Object^ owner) : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
#ifdef HANDLE
	_phCmsCouponPricer = NULL;
#endif
	_ppCmsCouponPricer = &childNative;
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppCmsCouponPricer));
}
Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (QuantLib::CmsCouponPricer& childNative, Object^ owner) : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
#ifdef HANDLE
	_phCmsCouponPricer = NULL;
#endif
	_ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (&childNative);
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppCmsCouponPricer));
    _CmsCouponPricerOwner = owner;
    _FloatingRateCouponPricerOwner = owner;
}

Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (CCmsCouponPricer^ copy) : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
#ifdef HANDLE
	_phCmsCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (copy->GetShared());
        _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppCmsCouponPricer));
    }
}
Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (PLATFORM::Type^ t) : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
#ifdef HANDLE
	_phCmsCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CCmsCouponPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (QuantLib::Handle<QuantLib::CmsCouponPricer>& childNative, Object^ owner)  : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
	_phCmsCouponPricer = &childNative;
	_ppCmsCouponPricer = &static_cast<boost::shared_ptr<QuantLib::CmsCouponPricer>>(childNative.currentLink());
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppCmsCouponPricer));
    _CmsCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (QuantLib::Handle<QuantLib::CmsCouponPricer> childNative)  : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
	_phCmsCouponPricer = &childNative;
	_ppCmsCouponPricer = &static_cast<boost::shared_ptr<QuantLib::CmsCouponPricer>>(childNative.currentLink());
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppCmsCouponPricer));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCmsCouponPricer::CCmsCouponPricer (QuantLib::CmsCouponPricer childNative)  : CFloatingRateCouponPricer(CCmsCouponPricer::typeid)
{
#ifdef HANDLE
	_phCmsCouponPricer = NULL;
#endif
	_ppCmsCouponPricer = new boost::shared_ptr<QuantLib::CmsCouponPricer> (new QuantLib::CmsCouponPricer (childNative));
    _ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCouponPricer> (*_ppCmsCouponPricer));
}
#endif

Cephei::QL::Cashflows::CCmsCouponPricer::~CCmsCouponPricer ()
{
    if (_ppCmsCouponPricer != NULL)
    {
	    delete _ppCmsCouponPricer;
        _ppCmsCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CCmsCouponPricer::!CCmsCouponPricer ()
{
    if (_ppCmsCouponPricer != NULL)
    {
	    delete _ppCmsCouponPricer;
    }
}
QuantLib::CmsCouponPricer& Cephei::QL::Cashflows::CCmsCouponPricer::GetReference ()
{
    if (_ppCmsCouponPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppCmsCouponPricer;
}
boost::shared_ptr<QuantLib::CmsCouponPricer>& Cephei::QL::Cashflows::CCmsCouponPricer::GetShared ()
{
    if (_ppCmsCouponPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppCmsCouponPricer;
}
QuantLib::CmsCouponPricer* Cephei::QL::Cashflows::CCmsCouponPricer::GetPointer ()
{
    if (_ppCmsCouponPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppCmsCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CmsCouponPricer>& Cephei::QL::Cashflows::CCmsCouponPricer::GetHandle ()
{
	if (_phCmsCouponPricer == NULL)
	{
		_phCmsCouponPricer = new Handle<QuantLib::CmsCouponPricer> (*_ppCmsCouponPricer);
	}
	return *_phCmsCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CCmsCouponPricer::HasNative () 
{
	return (_ppCmsCouponPricer != NULL);
}

Cephei::QL::Cashflows::ICmsCouponPricer^ Cephei::QL::Cashflows::CCmsCouponPricer::SetSwaptionVolatility::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCmsCouponPricer)->setSwaptionVolatility ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

